package com.atxwq.shop.common.intercepter;

import com.alibaba.fastjson.JSON;
import com.atxwq.shop.model.entity.user.UserInfo;
import com.atxwq.shop.model.vo.common.RedisCodeEnum;
import com.atxwq.utils.AuthContextUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @Description TODO
 * @Date 2025/5/22 20:29
 * @Created by 12336
 */

@Component
public class UserLoginAuthIntercepter implements HandlerInterceptor {
    @Autowired
    private RedisTemplate<String , String> redisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception{
        String token = request.getHeader("token");
        String userJson = redisTemplate.opsForValue().get(RedisCodeEnum.USER_LOGIN_TOKEN + token);
        AuthContextUtil.setUserInfo(JSON.parseObject(userJson,UserInfo.class));

        return true;
    }
}
